package jdbc0830;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginDemo {
    public static void main(String[] args) {
        int num=0;
        try (Connection con=DBUtil.getConnection()){

            Statement stmt=con.createStatement();

            UserInfo user=InputUtil.getInputObject(new UserInfo(),"登录用户","登录");

            String sql="SELECT id,username,password,nickname,age  "+
                    "FROM userinfo "+
                    "WHERE username='"+user.getUsername()+"' AND  password='"+user.getPassword()+"'";
            ResultSet rs=stmt.executeQuery(sql);
            if (rs.next()){
                System.out.println("欢迎用户："+rs.getString("nickname"));
            }else {
                System.out.println("用户或密码错误");
            }
//            String sql="SELECT username,password,nickname "+
//                    "FROM userinfo";
//            ResultSet rs=stmt.executeQuery(sql);
//            while (rs.next()){
//                String name=rs.getString("username");
//                String password= rs.getString("password");
//                if ((name.equals(user.getUsername())) && (password.equals(user.getPassword()))){
//
//                    num=1;
//                    break;
//                }
//            }
//            System.out.println(num>0?"欢迎 "+rs.getString("nickname")+" 用户登录":"用户名或密码错误");

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
